<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="460px" top="10vh" @close='close' class="ruleDialog">
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="130px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('对象名')" class="ruleTypeForm" prop="policyname">
          <el-select v-model="record.policyname" ref="policyname" size='small' filterable clearable>
            <el-option v-for="item in policynameList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('动作')" class="ruleTypeForm" prop="action">
          <el-select v-model="record.action" ref="action" size='small' filterable>
            <el-option v-for="(value, key) in actionList" :key="key" :label="$t('eventaction.' + value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('入侵防御策略名称')" class="ruleTypeForm" prop="ipspolicyname" v-if="record.action === 'se'">
          <el-select v-model="record.ipspolicyname" ref="ipspolicyname" size='small' clearable>
            <el-option v-for="item in ipspolicynameList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('病毒过滤策略名称')" class="ruleTypeForm" prop="avpolicyname" v-if="record.action === 'se'">
          <el-select v-model="record.avpolicyname" ref="avpolicyname" size='small' clearable>
            <el-option v-for="item in avpolicynameList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('URL策略名称')" class="ruleTypeForm" prop="urlpolicyname" v-if="record.action === 'se'">
          <el-select v-model="record.urlpolicyname" ref="urlpolicyname" size='small' clearable>
            <el-option v-for="item in urlpolicynameList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('深度过滤策略名称')" class="ruleTypeForm" prop="conpolicyname" v-if="record.action === 'se'">
          <el-select v-model="record.conpolicyname" ref="conpolicyname" size='small' clearable>
            <el-option v-for="item in conpolicynameList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getServiceArray, getServiceReArray } from '@/api/list/server'
import { getIpsArray } from '@/api/safe/ips'
import { getAvArray } from '@/api/safe/av'
import { getUrlArray } from '@/api/safe/url'
import { getWhiteList } from '@/api/safe/white'
import { getBlacklist, getBlacklistFile } from '@/api/safe/black'
import { selectClass } from '@/utils/input-class.js'
export default {
  name: 'TemplateRuleDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入策略名称')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        policyname: '',
        action: 'ACCEPT',
        ipspolicyname: '',
        avpolicyname: '',
        urlpolicyname: '',
        conpolicyname: ''
      },
      recordCopy: {
        policyname: '',
        action: 'ACCEPT',
        ipspolicyname: '',
        avpolicyname: '',
        urlpolicyname: '',
        conpolicyname: ''
      },
      actionList: { 'ACCEPT': '允许', 'se': '安全引擎' },
      policynameList: [],
      ipspolicynameList: [],
      avpolicynameList: [],
      urlpolicynameList: [],
      conpolicynameList: [],
      ruleNum: 0,
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  watch: {
    'record.action' (val) {
      if (val === 'se') {
        this.$nextTick(() => {
          const selectData = ['ipspolicyname', 'avpolicyname', 'urlpolicyname', 'conpolicyname']
          selectClass(this.$refs, selectData)
        })
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const selectData = ['policyname', 'action']
        selectClass(this.$refs, selectData)
      })
      this.ruleNum = data
      getServiceArray().then(response => {
        if (response.status === 0) {
          getServiceReArray().then(res => {
            if (res.status === 0) {
              this.policynameList = res.info.concat(response.info)
            }
          })
        }
      })
      getIpsArray().then(res => {
        if (res.status === 0) {
          this.ipspolicynameList = res.info
        }
      })
      getAvArray().then(res => {
        if (res.status === 0) {
          this.avpolicynameList = res.info
        }
      })
      getUrlArray().then(res => {
        if (res.status === 0) {
          this.urlpolicynameList = res.info
        }
      })
      Promise.all([
        getWhiteList(),
        getBlacklist(),
        getBlacklistFile()
      ]).then(res => {
        /* 你的逻辑代码 */
        if (res[0].status === 0 && res[1].status === 0 && res[2].status === 0) {
          this.conpolicynameList = res[0].info.concat(res[1].info.concat(res[2].info))
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var formData = Object.assign({}, this.record)
          if (formData.action === 'se' && !formData.ipspolicyname && !formData.avpolicyname && !formData.urlpolicyname && !formData.conpolicyname) {
            this.$messages.error(this.$t('至少添加一个策略！'))
          } else if (this.ruleNum < 10) {
            this.$emit('listData', formData)
            this.close()
          } else {
            this.$messages.error(this.$t('规则最多添加 10 个！'))
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.parentData = {}
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
      this.policynameList = []
      this.ipspolicynameList = []
      this.avpolicynameList = []
      this.urlpolicynameList = []
      this.conpolicynameList = []
      this.ruleNum = 0
    }
  }
}
</script>
<style lang="scss" scoped>
.tip-context {
  color: #999;
  font-size: 12px;
  line-height: 24px;
}
</style>
